<?php

/*
 * - Okek CMS
 * - create by Jg and Tito
 * - Okek Projects 2012
 * - CMS developed for free use
 * - More info http://habbo-dev.com
 */

class Core
{
    public function __construct() {
        if(!version_compare(PHP_VERSION, '5.4.0', '>=')) {
            
        }
    }
    
    public  function getConfig() {
        if(!file_exists(NEC . 'config.php'))
            return false;
        
        include NEC . 'config.php';
        $config = array($mysql, $site, $mus);
        return $config;
    }
    
    public function FilterText($str, $html = true, $e = 'ISO-8859-15')
    {
        if(is_array($str))
        {
            $final = array();

            foreach($str as $param => $value)
                $final[$param] = $this->FilterText($value, $html, $e);

            return $final;
        }

        if($this->isUtf8($str))
            $e = 'UTF-8';

        $str = stripslashes(trim($str));

        if($html)
            $str = htmlentities($str, ENT_QUOTES | ENT_SUBSTITUTE, $e, false);

        $str = mysql_real_escape_string($str);
        $str = str_replace('&amp;', '&', $str);
        $str = iconv($e, 'ISO-8859-15//TRANSLIT//IGNORE', $str);

        return nl2br($str);
    }
    
    function UTF8Decode($str)
	{
		if(!is_array($str))
			return utf8_decode($str);

		$final = array();

		foreach($str as $param => $value)
			$final[$param] = $this->UTF8Decode($value);

		return $final;
	}
    
    public function isUtf8($str) 
    { 
        $len = strlen($str); 

        for($i = 0; $i < $len; $i++)
        { 
            $c = ord($str[$i]);

            if ($c > 128) 
            { 
                if(($c > 247)) 
                    return false; 
                else if($c > 239) 
                    $bytes = 4; 
                else if($c > 223) 
                    $bytes = 3; 
                else if($c > 191)
                    $bytes = 2; 
                else 
                    return false; 

                if(($i + $bytes) > $len) 
                    return false; 

                while ($bytes > 1) 
                { 
                    $i++; 
                    $b = ord($str[$i]); 

                    if($b < 128 || $b > 191) 
                        return false;

                    $bytes--; 
                } 
            } 
        }

        return true; 
    }
    
    public function LoadJSON($file)
    {
        if(!file_exists($file))
                return false;

        $data 	= file_get_contents($file);
        $data 	= preg_replace('/\/\*(.*?)\*\//is', '', $data);
        $data 	= json_decode($data, true);

        $error 	= json_last_error();

        return (!empty($error)) ? $error : $data;
    }
    
    public function validateMail($email) {
        
        $isValid = true;
        $atIndex = strrpos($email, '@');

        if(is_bool($atIndex) && !$atIndex){
            $isValid = false;
        }
        else {
            $domain = substr($email, $atIndex+1);
            $local = substr($email, 0, $atIndex);
            $localLen = strlen($local);
            $domainLen = strlen($domain);

            if ($localLen < 1 || $localLen > 64){
                $isValid = false;
            }else if ($domainLen < 1 || $domainLen > 255){
                $isValid = false;
            }else if ($local[0] == '.' || $local[$localLen-1] == '.'){
                $isValid = false;
            }else if (preg_match('/\\.\\./', $local)){
                $isValid = false;
            }else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)){
                $isValid = false;
            }else if (preg_match('/\\.\\./', $domain)){
                $isValid = false;
            }else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
                if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))){
                $isValid = false;
                }
            }
            if ($isValid && !(checkdnsrr($domain,'MX') || checkdnsrr($domain,'A'))){
                $isValid = false;
            }
        }

        return $isValid;						
    }
}
?>
